package lzy.controller;

import lzy.entity.User;
import lzy.service.BatchService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import lzy.service.UserService;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import lzy.service.FarmOperationService;
import lzy.service.ProcessingEquipmentService;

@Controller
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private BatchService batchService;

    @Autowired
    private FarmOperationService farmOperationService;

    @Autowired
    private ProcessingEquipmentService processingEquipmentService;

    @GetMapping("/admin/system_overview")
    public String systemOverview(HttpSession session, RedirectAttributes redirectAttributes, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            redirectAttributes.addFlashAttribute("error", "请先登录！");
            return "redirect:/login";
        }
        if (!"ADMIN".equals(user.getRole())) {
            redirectAttributes.addFlashAttribute("error", "您没有权限访问此页面！");
            return "redirect:/login";
        }
        
        // 添加系统概览所需的数据
        int userCount = userService.countAll();
        model.addAttribute("totalUsers", userCount); // 用户总数
        int harvestCount = farmOperationService.countHarvest();
        model.addAttribute("totalBatches", harvestCount); // 生产批次=采收数

        int weekChecked = batchService.countBatchCheckedIn7Days();

        model.addAttribute("totalProcessingBatches", weekChecked);
        int equipmentCount = processingEquipmentService.countAll();
        model.addAttribute("totalEquipment", equipmentCount);
        
        return "admin/system_overview";
    }
    
    @GetMapping("/admin/dashboard")
    public String adminDashboard(HttpSession session, RedirectAttributes redirectAttributes) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            redirectAttributes.addFlashAttribute("error", "请先登录！");
            return "redirect:/login";
        }
        if (!"ADMIN".equals(user.getRole())) {
            redirectAttributes.addFlashAttribute("error", "您没有权限访问此页面！");
            return "redirect:/login";
        }
        return "admin/dashboard";
    }

    @GetMapping("/admin/user_management")
    public String userManagement(@RequestParam(required = false) String role, HttpSession session, RedirectAttributes redirectAttributes, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            redirectAttributes.addFlashAttribute("error", "请先登录！");
            return "redirect:/login";
        }
        if (!"ADMIN".equals(user.getRole())) {
            redirectAttributes.addFlashAttribute("error", "您没有权限访问此页面！");
            return "redirect:/login";
        }
        List<String> roleList = Arrays.asList("ADMIN", "PROCESSING", "PRODUCING");
        model.addAttribute("roleList", roleList);
        List<User> userList = (role == null || role.isEmpty()) ? userService.findAll() : userService.findByRole(role);
        model.addAttribute("userList", userList);
        int userCount = userService.countAll();
        model.addAttribute("userCount", userCount);
        model.addAttribute("selectedRole", role);
        return "admin/user_management";
    }

    @GetMapping("/admin/production_management")
    public String productionManagement(HttpSession session, RedirectAttributes redirectAttributes) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            redirectAttributes.addFlashAttribute("error", "请先登录！");
            return "redirect:/login";
        }
        if (!"ADMIN".equals(user.getRole())) {
            redirectAttributes.addFlashAttribute("error", "您没有权限访问此页面！");
            return "redirect:/login";
        }
        return "admin/production_management";
    }

    @GetMapping("/admin/processing_management")
    public String processingManagement(HttpSession session, RedirectAttributes redirectAttributes) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            redirectAttributes.addFlashAttribute("error", "请先登录！");
            return "redirect:/login";
        }
        if (!"ADMIN".equals(user.getRole())) {
            redirectAttributes.addFlashAttribute("error", "您没有权限访问此页面！");
            return "redirect:/login";
        }
        return "admin/processing_management";
    }

    @PostMapping("/admin/addUser")
    @ResponseBody
    public String addUser(@RequestBody User user) {
        userService.addUser(user);
        return "success";
    }

    @PostMapping("/admin/deleteUser")
    @ResponseBody
    public String deleteUser(@RequestParam Long id) {
        try {
            userService.deleteUserById(id);
            return "success";
        } catch (Exception e) {
            return "fail";
        }
    }
} 